<template>
<view class="allowancesFill">
    <view class="title">基本信息</view>
    <view class="uni-list">
        <view class="uni-list-cell">
            <view class="uni-list-cell-left">
                <image src="/static/img/street.png" class="down_img"></image><text>村/社区</text>
            </view>
            <view class="uni-list-cell-db" style="width:70%">
                <picker mode="multiSelector" @change="bindMultiPickerChange" @columnchange="bindMultiPickerColumnChange" :value="multiIndex" :range="multiArray" range-key="unit_name">
                    <text class="uni-input">{{multiArray[0][multiIndex[0]].unit_name || ''}} - {{multiArray[1][multiIndex[1]].unit_name || ''}}</text>
                    <image src="/static/img/right.png" class="right_img"></image>
                </picker>
            </view>
        </view>
        <view class="uni-list-cell">
            <view class="uni-list-cell-left">
                <image src="/static/img/drop-down.png" class="down_img"></image><text>保障类型</text>
            </view>
            <view class="uni-list-cell-db">
                <text class="uni-input">{{caidData.CLASS1}}</text>
            </view>
        </view>
        <view class="uni-list-cell">
            <view class="uni-list-cell-left">
                <image src="/static/img/drop-down.png" class="down_img"></image><text>救助类别</text>
            </view>
            <view class="uni-list-cell-db">
                <text class="uni-input">{{caidData.CLASS2}}</text>
            </view>
        </view>
    </view>
    <view class="title add" @click="addSecurityPersonnel('保障人员')">保障人员<image src="/static/img/add.png"></image></view>
    <view class="personnel">
        <view class="item" v-for="(item, index) in caidData.DT_DETAIL" :key="index" v-show="item.LD_ROLE === '保障人员'" @click="editDTDETAIL(index)">
            <image src="/static/img/default.png" v-if="!item.LD_IMG"></image>
            <image :src="$host+item.LD_IMG" v-else></image>
            <view class="user_info">
                <text class="name">姓名：{{item.LD_NAME}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{item.LD_REL}}</text>
                <text>性别：{{item.SEX}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;生日：{{item.LD_BIRTHDAY}}</text>
                <text>身份证：{{item.LD_ID}}</text>
            </view>
            <image src="/static/img/reduction.png" class="reduction" @click.stop="removeDTDETAIL(index)"></image>
        </view>
        <!-- <view class="item measure">测算收入：200元/月</view> -->
    </view>
    <view class="title add" @click="addSecurityPersonnel('赡养人员')">赡养人<image src="/static/img/add.png"></image></view>
    <view class="personnel">
        <view class="item" v-for="(item, index) in caidData.DT_DETAIL" :key="index" v-show="item.LD_ROLE === '赡养人员'" @click="editDTDETAIL(index)">
            <image src="/static/img/default.png" v-if="!item.LD_IMG"></image>
            <image :src="$host+item.LD_IMG" v-else></image>
            <view class="user_info">
                <text class="name">姓名：{{item.LD_NAME}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{item.LD_REL}}</text>
                <text>性别：{{item.SEX}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;生日：{{item.LD_BIRTHDAY}}</text>
                <text>身份证：{{item.LD_ID}}</text>
            </view>
            <image src="/static/img/reduction.png" class="reduction" @click.stop="removeDTDETAIL(index)"></image>
        </view>
        <!-- <view class="item measure">测算收入：200元/月</view> -->
    </view>
    <view class="title add" @click="budget('收入')">家庭收入<image src="/static/img/add.png"></image></view>
    <view class="income">
        <view class="item" v-for="(item, index) in caidData.DT_INCOME" :key="index" v-show="item.LI_TYPE === '收入'" @click="editbudget(index)">
            <text class="type">{{item.LI_SUB}}</text>
            <text>{{item.LI_ITEM}}</text>
            <text class="money">{{item.LI_MONEY}}元/{{item.LI_UNIT}}</text>
            <image src="/static/img/removeImg.jpg" @click.stop="removeImg(index)" class="removeImg"></image>
        </view>
    </view>
    <view class="title add" @click="budget('支出')">家庭支出<image src="/static/img/add.png"></image></view>
    <view class="income">
        <view class="item" v-for="(item, index) in caidData.DT_INCOME" :key="index" v-show="item.LI_TYPE === '支出'" @click="editbudget(index)">
            <text class="type">{{item.LI_SUB}}</text>
            <text>{{item.LI_ITEM}}</text>
            <text class="money">{{item.LI_MONEY}}元/{{item.LI_UNIT}}</text>
             <image src="/static/img/removeImg.jpg" @click.stop="removeImg(index)" class="removeImg"></image>
        </view>
    </view>
    <view class="title">评估家庭收支情况</view>
    <view class="situation">
        <text>工资性收入：{{predict.MONEY_WAGE || 0}}元/月</text>
        <text>赡养费：{{predict.MONEY_SUPPORT || 0}}元/月</text>
        <text>经营性收入：{{predict.MONEY_MANAGER || 0}}元/月</text>
        <text>财产性收入：{{predict.MONEY_PROPERTY || 0}}元</text>
        <text>转移性收入：{{predict.MONEY_TRANSFER || 0}}元/月</text>
        <text>医疗费用：{{predict.COST_MEDICAL || 0}}元/月</text>
        <text>教育费用：{{predict.COST_TEACH || 0}}元/月</text>
		<text>护理费：{{predict.COST_NURSING || 0}}元/月</text>
        <text class="right">测算家庭收入：{{predict.AMOUNT_INCOME || 0}}元/月</text>
    </view>
    <button @click="goEditallowances" class="but" v-if="conform">测算符合低保条件，点此提交、申请</button>
    <button class="but red" v-else>不符合低保条件</button>
</view>
</template>

<script>
import { formatDate } from '@/utils/date.js'
  export default {
    data() {
        return {
            conform: false,
            predict:{},
            $host: this.$host,
            caidData: {
                LM_GUID:'',
                YM:'',
                MT:'新增',
                CLASS1:'',
                CLASS2:'',
                UNIT_ID:'',
                UNIT_NAME:'',
                DT_DETAIL:[],
                DT_INCOME:[],
                WXID:''
            },
            multiIndex: [0,0],
            village: {},
            community: {},
            multiArray: [],
            getUnitList: []
        }
    },
    onLoad(options) {
        let that = this;
        uni.getStorage({
            key: 'openid',
            success: function (res) {
                that.caidData.WXID = res.data
            }
        });
        console.log(JSON.parse(decodeURIComponent(options.data)))
        if (options.edit) {
            let data = JSON.parse(decodeURIComponent(options.data))
            data.DTDETAIL.forEach(item => {
                item.LD_CLASS2IMG = item.LD_CLASS2IMG?item.LD_CLASS2IMG.split(","):item.LD_CLASS2IMG
                item.LD_WAGEIMG = item.LD_WAGEIMG?item.LD_WAGEIMG.split(","):item.LD_WAGEIMG
                item.LD_PENSIMG = item.LD_PENSIMG?item.LD_PENSIMG.split(","):item.LD_PENSIMG
                item.LD_MZ = item.LD_NATION;
                if (item.FACTORS) {
                    item.FACTORS.forEach(element => {
                        element.IMG = element.IMG?element.IMG.split(","):element.IMG
                    });
                }
            });
            data.DTINCOME.forEach(item => {
                item.LI_IMG = item.LI_IMG?item.LI_IMG.split(","):item.LI_IMG
            });
            this.caidData.LM_GUID = data.MASTER.LM_GUID;
            this.caidData.YM = data.MASTER.YM;
            this.caidData.MT = data.MASTER.MT;
            this.caidData.CLASS1 = data.MASTER.CLASS1;
            this.caidData.CLASS2 = data.MASTER.CLASS2;
            this.caidData.UNIT_ID = data.MASTER.UNIT_ID;
            this.caidData.UNIT_NAME = data.MASTER.UNIT_NAME;
            this.caidData.DT_DETAIL = data.DTDETAIL;
            this.caidData.DT_INCOME = data.DTINCOME;
            this.caidData.WXID = data.MASTER.WXID;
            console.log(this.caidData)
        } else {
            let data = JSON.parse(decodeURIComponent(options.data))
            data.LD_REL = '户主';
            data.LD_ROLE = '保障人员';
            data.LD_CLASS2 = data.CLASS2;
            this.caidData.CLASS1 = data.CLASS1
            this.caidData.CLASS2 = data.CLASS2
            this.caidData.LM_GUID = data.LM_GUID
            this.caidData.DT_DETAIL.push(data)
        }
        this.getGetUnitList()
        this.getNotice()
    },
    onShow(e) {
        this.caidData.DT_DETAIL.splice(0,0)
        this.caidData.DT_INCOME.splice(0,0)
        // if (this.caidData.DT_DETAIL.length) {
        this.postCheckLow()
        // }
    },
    methods: {
        // 获取最低标准
        getNotice() {
            this.$http.get('notice', {params: {CLASS1: '', CLASS2: ''}}).then(res => {
                this.nav = res.data;
                res.data.forEach(item => {
                    if (item.CLASS1 == this.caidData.CLASS1 && item.CLASS2 == this.caidData.CLASS2) {
                        uni.setStorage({
                            key: 'MIN_WAGE_LINE',  
                            data: item.MIN_WAGE_LINE
                        })
                    }
                });
            }).catch(err => {
            })
        },
        // 删除保障人员与赡养人
        removeDTDETAIL (index) {
            let that = this;
            uni.showModal({
                title: '提示',
                content: '确定要删除该成员吗？',
                success: function (res) {
                    if (res.confirm) {
                        that.caidData.DT_DETAIL.splice(index,1)
                    } else if (res.cancel) {
                    }
                }
            });
            
        },
        // 添加保障人员与赡养人
        addSecurityPersonnel(type) {
            uni.navigateTo({
                url: 'memberEntry?LM_GUID=' + this.caidData.LM_GUID + '&LD_ROLE=' + type + '&isAdd=' + 0
            })
        },
        // 编辑保障人员与赡养人
        editDTDETAIL (index) {
            let data = this.caidData.DT_DETAIL[index];
            uni.navigateTo({
                url: 'memberEntry?data=' + encodeURIComponent(JSON.stringify(data)) + '&index=' + index + '&isAdd=' + 1
            })
        },
        // 添加收入与支出
        budget(type) {
            uni.navigateTo({
                url: 'closedPay?LM_GUID=' + this.caidData.LM_GUID + '&LI_TYPE=' + type + '&isAdd=' + 0
            })
        },
        // 编辑收入与支出
        editbudget(index) {
            let data = this.caidData.DT_INCOME[index];
            uni.navigateTo({
                url: 'closedPay?data=' + encodeURIComponent(JSON.stringify(data)) + '&index=' + index + '&isAdd=' + 1
            })
        },
        // 街道选择
        bindMultiPickerChange(e) {
            this.multiIndex = e.detail.value
            this.village = this.multiArray[0][e.detail.value[0]]
            if (e.detail.value[1] != null) {
                this.community = this.multiArray[1][e.detail.value[1]]
            } else {
                this.community = {}
            }
        },
        bindMultiPickerColumnChange(e) {
            if (e.detail.column) {
                return false
            }
            let community = []
            this.getUnitList.forEach(item => {
                if (item.parent_unit === this.multiArray[0][e.detail.value].unit_id) {
                    community.push(item)
                }
            });
            this.multiArray[1] = community
            this.multiArray.push()
        },
        // 获取单位信息
        getGetUnitList () {
            this.$http.get('GetUnitList').then(res => {
                let village = []
                let community = []
                let multiArray = []
                this.getUnitList = res.data
                res.data.forEach(item => {
                    if (item.parent_unit == 0) {
                        village.push(item)
                    }
                    if (item.parent_unit === village[0].unit_id) {
                        community.push(item)
                    }
                });
                multiArray[0] = village
                multiArray[1] = community
				this.multiArray = multiArray
            })
        },
        // 删除收入支出
        removeImg (index) {
            let that = this;
            uni.showModal({
                title: '删除提示',
                content: '是否确定删除该项？',
                success: function (res) {
                    if (res.confirm) {
                        that.caidData.DT_INCOME.splice(index,1)
                    }
                }
            });
        },
        // 是否符合
        postCheckLow () {
            uni.showLoading({
                title: '正在测算...'
            });
            let that = this;
            let objString = JSON.stringify(this.caidData);
            let copydata = JSON.parse(objString);
            copydata.YM = formatDate(new Date(),'yyyyMM')
            copydata.DT_DETAIL.forEach(item => {
                item.LD_CLASS2IMG = item.LD_CLASS2IMG?item.LD_CLASS2IMG.join(","):item.LD_CLASS2IMG
                item.LD_WAGEIMG = item.LD_WAGEIMG?item.LD_WAGEIMG.join(","):item.LD_WAGEIMG
                item.LD_PENSIMG = item.LD_PENSIMG?item.LD_PENSIMG.join(","):item.LD_PENSIMG
                item.LD_NATION = item.LD_MZ;
                if (item.FACTORS) {
                    item.FACTORS.forEach(element => {
                        element.IMG = element.IMG?element.IMG.join(","):element.IMG
                    });
                }
            });
            copydata.DT_INCOME.forEach(item => {
                item.LI_IMG = item.LI_IMG?item.LI_IMG.join(","):item.LI_IMG
            });
            copydata.DT_DETAIL = JSON.stringify(copydata.DT_DETAIL)
            copydata.DT_INCOME = JSON.stringify(copydata.DT_INCOME)
            this.$http.post('CheckLow',{...copydata}).then(res => {
                that.predict = res.data.ana
                if (res.data.code == 0) {
                    that.conform = true
                } else {
					that.conform = false
				}
                uni.hideLoading();
            })
        },
        // 低保申报
        goEditallowances () {
            let that = this;
			uni.showModal({
			    title: '是否提交',
			    content: '根据您当前申报的材料，系统测算您符合条件，请确保您的申报材料已经完整且真实，提交后，民政工作人员将进行入户核实！如果发现您有虚假申报将严肃处理，确定继续提交吗？',
			    success: function (res) {
			        if (res.confirm) {
						uni.showLoading({
						    title: '正在提交...'
						});
						let objString = JSON.stringify(that.caidData);
						let copydata = JSON.parse(objString);
						copydata.UNIT_ID = that.multiArray[1][that.multiIndex[1]].unit_id;
						copydata.UNIT_NAME = that.multiArray[1][that.multiIndex[1]].unit_name;
						copydata.YM = formatDate(new Date(),'yyyyMM');
						copydata.DT_DETAIL.forEach(item => {
							item.LD_CLASS2IMG = item.LD_CLASS2IMG?item.LD_CLASS2IMG.join(","):item.LD_CLASS2IMG
							item.LD_WAGEIMG = item.LD_WAGEIMG?item.LD_WAGEIMG.join(","):item.LD_WAGEIMG
							item.LD_PENSIMG = item.LD_PENSIMG?item.LD_PENSIMG.join(","):item.LD_PENSIMG
							item.LD_NATION = item.LD_MZ;
							if (item.FACTORS) {
								item.FACTORS.forEach(element => {
									element.IMG = element.IMG?element.IMG.join(","):element.IMG
								});
							}
						});
						copydata.DT_INCOME.forEach(item => {
							item.LI_IMG = item.LI_IMG?item.LI_IMG.join(","):item.LI_IMG
						});
						copydata.DT_DETAIL = JSON.stringify(copydata.DT_DETAIL)
						copydata.DT_INCOME = JSON.stringify(copydata.DT_INCOME)
						that.$http.post('LowSumit',{...copydata}).then(res => {
							if (res.data.code == 0) {
								uni.showModal({
								    title: '提交成功',
								    content: '请等待民政工作人员核实，您也可以查询办理进度。',
									showCancel: false,
								    success: function (res) {
								        if (res.confirm) {
								            uni.reLaunch({
								            	url: '/pages/notice/navlist'
								            });
								        }
								    }
								});
							} else {
								uni.showModal({
									title: '提示',
									showCancel: false,
									content: res.data.message
								});
							}
							uni.hideLoading();
						});
					} else if (res.cancel) {
					}
				}
			});
		}
    }
  }
</script>

<style lang="scss" scoped>
.allowancesFill{
    background-color: #F0EEEE;
    padding-bottom: 100upx;
    .title{
        font-size: 28upx;
        color: #101010;
        height: 90upx;
        line-height: 90upx;
        width: 100%;
        padding:0upx 20upx;
        box-sizing: border-box;
        background-color: #F0EEEE;
        &.add{
            display: flex;
            align-items: center;
            justify-content: space-between;
            image{
                width: 48upx;
                height: 48upx;
            }
        }
    }
    .uni-list-cell{
        border-top: solid 1px #DCDFE6;
        border-bottom: solid 1px #DCDFE6;
        margin: 12upx 0;
        display: flex;
        height: 100upx;
        align-items: center;
        font-size: 32upx;
        color: #6D6E71;
        padding: 0 30upx;
        justify-content: space-between;
        background-color: #fff;
        .uni-list-cell-left{
            .down_img{
                margin-right: 20upx;
                width: 44upx;
                height: 44upx;
                vertical-align: middle;
            }
            text{
                vertical-align: middle;
            }
        }
        .uni-list-cell-db{
            width: 50%;
            text-align: right;
        }
        .right_img{
            width: 50upx;
            height: 50upx;
            vertical-align: middle;
        }
        .uni-input{
            vertical-align: middle; 
        }
    }
    .personnel{
        background-color: #fff;
        .item{
            padding: 30upx;
            box-sizing: border-box;
            border-bottom: solid 1px #DCDFE6;
            position: relative;
            image{
                width: 130upx;
                height: 150upx;
                display: inline-block;
                margin-right: 30upx;
                &.reduction{
                    width: 48upx;
                    height: 48upx;
                    position: absolute;
                    top: 70upx;
                    right: 20upx;
                }
            }
            &.measure{
                text-align: right;
                border-bottom: none;
                font-size: 28upx;
                color: #E51C23;
            }
            .user_info{
                color: #666;
                display: inline-block;
                font-size: 28upx;
                vertical-align:top;
                text{
                    margin-bottom: 10upx;
                    display: block;
                }
                .name{
                    font-size: 30upx;
                    color: #222;
                }
            }
        }
    }
    .income{
        background-color: #fff;
        display: flex;
        // justify-content: space-between;
        // margin: 20upx;
        font-size: 28upx;
        color: #101010;
        align-items: center;
        text-align: center;
        .item{
            padding: 30upx;
            position: relative;
            .removeImg{
                width: 36upx;
                height: 36upx;
                position: absolute;
                right: 6upx;
                top: -0;
                border: 0;
                margin: 0;
                padding: 0;
                z-index: 2;
            }
        }
        text{
            display: block;
        }
        .type{
            margin-bottom: 10upx;
            font-weight: bold;
        }
        .money{
            font-weight: bold;
            color: #E51C23;
            margin-top: 10upx;
        }
        image{
            width: 120upx;
            height: 120upx;
            margin-right: 100upx;
            border: solid 1px #ccc;
            padding: 20upx;
            box-sizing: border-box;
        }
    }
    .situation{
        background-color: #fff;
        display: flex;
        flex-direction: column;
        padding: 20upx;
        font-size: 28upx;
        text-align: left;
        text{
            margin-bottom: 20upx;
            display: block;
        }
        .right{
            color: #E51C23;
            text-align: right;
            margin-top: 20upx;
        }
    }
    .but{
        background-color: #1EC88B;
        border: none;
        height: 94upx;
        line-height: 94upx;
        text-align: center;
        border-radius: 10upx;
        color: #fff;
        margin:40upx 20upx 0upx 20upx;
        font-size: 36upx;
        &.red {
            background-color: #E51C23;
        }
    }
}
</style>